package com.hxx.day8;

import org.junit.Test;

import java.io.*;

public class TestDemo {
    /**
     * 字节输入流
     */
    @Test
    public void test1(){
        //自动关闭
        try (InputStream inputStream = new FileInputStream("d:/zmx/one.txt");){
            //一次读取一个字节
            int tmp = 0;
            while ((tmp=inputStream.read()) != -1){
                System.out.println((char) tmp);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 字节输出流
     */
    @Test
    public void test2(){
        try (OutputStream outputStream = new FileOutputStream("d:/zmx/two.txt");){
            outputStream.write("hello world".getBytes());
            //刷新缓冲区
            outputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 完成文件的复制
     */
    @Test
    public void test3(){
        try (InputStream inputStream = new FileInputStream("d:/zmx/one.txt");
             OutputStream outputStream = new FileOutputStream("d:/zmx/three.txt");){
            int tmp = 0;
            while ((tmp=inputStream.read()) != -1){
                outputStream.write(tmp);
            }
            //刷新缓冲区
            outputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 字符输入输出流
     */
    @Test
    public void test4(){
        try (Reader reader = new FileReader("d:/zmx/one.txt");){
           int tmp = 0;
           //一次读取一个字符
           while ((tmp=reader.read()) != -1){
               System.out.println((char) tmp);
           }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 字符输出流
     */
    @Test
    public void test5(){
        try (Writer writer = new FileWriter("d:/zmx/four.txt");){
            writer.write("好");
            //刷新缓冲区
            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * 复制文件
     */
    @Test
    public void test6(){
        try (Reader reader = new FileReader("d:/zmx/two.txt");
             Writer writer = new FileWriter("d:/zmx/five.txt");){
            int tmp = 0;
            //一次读取一个字符
            while ((tmp=reader.read()) != -1){
                writer.write(tmp);
            }
            //刷新缓冲区
            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 使用字符流完成文件的复制（上传）
     */
    @Test
    public void test7(){
        try (InputStream reader = new FileInputStream("d:/zmx/two.txt");
             OutputStream writer = new FileOutputStream("d:/zmx/six.txt");){
            byte[] buf = new byte[1024];
            int len = 0;
             //一次读取一个字符
             while ((len=reader.read(buf)) != -1){
                 writer.write(buf,0,len);
             }
            //刷新缓冲区
            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}





































